<?php
require_once("../../../config.inc.php");
$isReloadable = false;
require_once("../../../app/_inc/include.php");
require_once("../_inc/_aValFields.php");
require_once("./_inc/layoutFunction.php");

$errMsg = "";
$layout = sxREQUEST("layout");
$band = sxREQUEST("band");

if(sxREQ("reqtype") == "save") {
	$errMsg .= layoutBandFieldIns();
}

$alias_entity = sxREQ('alias_entity');
if ($alias_entity != '' and $alias_entity[0] != 'W') {
		//load attribute of alias_entity selected
		$sSQL = "SELECT a.* FROM layout_alias la" .
				" INNER JOIN attribute a ON a.entity = la.entity" .
				" WHERE la.layout_alias='".$alias_entity[0]."'" .
				" ORDER BY a.sequence";
		$objSDAO->query($sSQL, "Attr");
		if ($attribute == '') {
			$rowAtt = $objSDAO->fetch("Attr");
			$attribute = $rowAtt['attribute'];
		}	
} else {
	$attribute = ''; 
}
$rowLookup = false;
if($alias_entity != '' and $attribute != '') {
//load variable or attribute of layout_alias selected
	$sSQL = "SELECT alu.attribute " .
			" FROM attribute a " .
			" INNER JOIN domain d ON d.domain = a.domain " .
			" INNER JOIN attribute alu ON alu.entity = d.lookup_entity " .
			" WHERE a.entity = '".substr($alias_entity,2)."' AND a.attribute='".$attribute."'" .
			" ORDER BY alu.sequence";
	$objSDAO->query($sSQL, "lookup");
	$rowLookup = $objSDAO->fetch("lookup");
}

if (sxREQ('row')=='') {
	//set band, row & seq with last values
	$condBand = '';
	if ($band != '') $condBand = " AND a.band = $band ";
	$sSQL = "SELECT a.*" .
			" FROM layout_field a " .
			" WHERE a.layout= '$layout' $condBand ".
			" ORDER BY a.band DESC,a.row DESC,a.seq DESC";
	$objSDAO->query($sSQL, "brs");
	$rbrs = $objSDAO->fetch("brs");
	if($rbrs){
		$band=$rbrs["band"];
		$_REQUEST['row']=$rbrs["row"];
		$_REQUEST['seq']=$rbrs["seq"];
	}else{
		$_REQUEST['row']=10;
		$_REQUEST['seq']=10;
	}	
}

$sSQL = "SELECT lv.*, l.max_colspan, l.type FROM layout l " .
		" LEFT JOIN layout_variable lv ON l.layout = lv.layout " .
		" WHERE l.layout = '$layout'" .
		" ORDER BY lv.variable";
$objSDAO->query($sSQL, "Variab");
$rowVar = $objSDAO->fetch("Variab",0);
$max_colspan = $rowVar['max_colspan'];
$ls_type = $rowVar['type'];
//load layout_alias
$sSQL = "SELECT * FROM layout_alias WHERE layout='$layout' ORDER BY layout_alias";
$objSDAO->query($sSQL, "layout_alias");

$aOpz = array("kMenu"=>'pro.lay', "isReloadable"=>$isReloadable);
echo(headerHTML($aOpz));
echo(bodyHTML($aOpz));
?>
<script language="JavaScript" type="text/javascript">
function objFrmSubm(rt){
	document.objFrm.reqtype.value = rt;
	document.objFrm.submit();
}
</script>
<?php

echo getLOHeader($layout);

$aCmd = array("insMD"=>array("pag"=>"javascript:objFrmSubm('save');",
										"title"=>_t("save"),
										"text"=>_t("save")
										),
					"back"=>array()
					);
$sCmd = (commandsHTML($aCmd));	
echo $sCmd;

echo getHtmlForm('objFrm');
echo getHtmlHidden('xsn',XSN);
echo getHtmlHidden('reqtype','...');
echo getHtmlHidden('layout');
echo getHtmlHidden('ins_band');

echo("<table class=\"tabl01\" width=\"100%\" cellpadding=\"3\" cellspacing=\"2\" border='0'>\n");
echo("<tr>" .
			"<td class=\"titl01\" colspan='6'>Campo</td>" .
		"</tr>\n");
echo("<tr>" .
			"<td class=\"labl01\" colspan='1'>Band*</td>" .
			"<td class=\"text01\" colspan='1'>");
if($ls_type=='table-line') {
	echo "0001".getHtmlHidden('band','1');
} elseif (sxREQ('ins_band') != 'S') {
	echo str_pad($band,4,0,STR_PAD_LEFT);
	echo getHtmlHidden('band');
} else {
	echo getHtmlText('band',4,4);
}
echo("</td>" .
			"<td class=\"labl01\">Row*</td>" .
			"<td class=\"text01\" colspan='1'>");
if($ls_type=='table-line') {
	echo "0001".getHtmlHidden('row','1');
} else {
	echo getHtmlText('row',4,4);
}
echo("</td><td class=\"labl01\">Sequence*</td>" .
			"<td class=\"text01\" colspan='1'>" .
			getHtmlText('seq',5,5)."</td>" .
		"</tr>\n");
// label
echo("<tr>" .
			"<td class=\"titl01\" colspan='6'>Label</td>" .
		"</tr>\n");
if($ls_type!='table-line') {
	//label colspan
	echo("<tr>" .
				"<td class=\"labl01\">Number of slots</td>" .
				"<td class=\"text01\" colspan='5'><table cellpadding='0' cellspacing='0'><tr>");
	for($k=0; $k<=$max_colspan; $k++) {
		echo("<td class=\"text01\" style='text-align:center;width:30px;font-size:8pt'>$k</td>");
	}
	echo("</tr><tr>");
	for($k=0; $k<=$max_colspan; $k++) {
		$chk = ($label_colspan == $k)?' CHECKED':'';
		echo("<td class=\"text01\" align='center'>" .
				"<input class=\"text01\" type=\"radio\" name=\"label_colspan\" value=\"$k\"$chk></td>\n");
	}
	echo("</tr></table></td>" .
			"</tr>\n");
} else {
	echo("<input type='hidden' name='label_colspan' id='label_colspan' value='0'");
}
//label
echo("<tr>" .
			"<td class=\"labl01\">Text</td>" .
			"<td class=\"text01\" colspan='5'>" .
			"<textarea class=\"inpt01\" name=\"label\" id=\"label\" cols=\"80\" rows=\"1\">".sxHE($label)."</textarea></td>" .
		"</tr>\n");		
// label properties
echo("<tr>" .
			"<td class=\"labl01\" width='20%'>Properties</td>" .
			"<td class=\"text01\" width='20%'>");
echo("<select name=\"label_attr\" id=\"label_attr\" class='inpt01'>");
foreach($aValLabelAttr as $attr){
	$sel = "";
	if($attr == $label_attr)
		$sel = "SELECTED";
	echo("<option value=\"$attr\" $sel>$attr</option>");
}
echo("</select></td>\n");
echo("<td class=\"labl01\" width='10%'>Alignement</td>" .
			"<td class=\"text01\" width='20%'>");
echo("<select name=\"label_align\" id=\"label_align\" class='inpt01'>");
foreach($aValLabelAlign as $attr){
	$sel = "";
	if($attr == $label_align)
		$sel = "SELECTED";
	echo("<option value=\"$attr\" $sel>$attr</option>");
}
echo("</select></td>\n");
echo("<td class=\"labl01\" width='10%'>Style</td>" .
			"<td class=\"text01\" width='20%'>");
echo("<select name=\"label_style\" id=\"label_style\" class='inpt01'>");
foreach($aValLabelStyle as $attr){
	$sel = "";
	if($attr == $label_style)
		$sel = "SELECTED";
	echo("<option value=\"$attr\" $sel>$attr</option>");
}
echo("</select>");
echo("</td>" .
		"</tr>\n");
// value
echo("<tr>" .
			"<td class=\"titl01\" colspan='6'>Value</td>" .
		"</tr>\n");
// colspan 
echo("<tr>" .
			"<td class=\"labl01\">Number of slots</td>" .
			"<td class=\"text01\" colspan='5'><table cellpadding='0' cellspacing='0'><tr>");
for($k=0; $k<=$max_colspan; $k++) {
	echo("<td class=\"text01\" style='text-align:center;width:30px;font-size:8pt'>$k</td>");
}
echo("</tr><tr>");
for($k=0; $k<=$max_colspan; $k++) {
	$chk = ($field_colspan == $k)?' CHECKED':'';
	echo("<td class=\"text01\" align='center'><input class=\"text01\" type=\"radio\" name=\"field_colspan\" value=\"$k\"$chk></td>\n");
}
echo("</tr></table></td>" .
		"</tr>\n");
// alias - attribute / variable
echo("<tr>" .
			"<td class=\"labl01\">Entity</td>" .
			"<td class=\"text01\" colspan='1'>");
echo("<select name=\"alias_entity\" id=\"alias_entity\" onChange=\"objFrmSubm('reload');\" class='inpt01'>");
echo("<option value=\"\">-----</option>");
while($rowRS = $objSDAO->fetch("layout_alias")){
	if ($rowRS["layout_alias"] == 'X'){ continue; } 
	$sel = "";
	if($alias_entity == $rowRS["layout_alias"].'_'.$rowRS["entity"])
		$sel = "SELECTED";
	echo("<option value=\"".$rowRS["layout_alias"].'_'.$rowRS["entity"]."\" $sel>".
		$rowRS["layout_alias"].'_'.(($rowRS["layout_alias"]!='W')?$rowRS["entity"]:'(work-variable)')."</option>");
}
$sel = "";
if($alias_entity == "W") $sel = "SELECTED";
echo("<option value=\"W\" $sel>(work-variable)</option>");
echo("</select>");
echo("</td>\n");
if($alias_entity == ""){
	echo("<td class=\"labl01\">&nbsp;</td>" .
			"<td class=\"text01\" colspan='1'>-- spaces --</td>\n");
} elseif($alias_entity[0] == "W"){
	echo("<td class=\"labl01\">Variable</td>" .
			"<td class=\"text01\" colspan='1'>\n");
	echo("<select name=\"variable\" id=\"variable\" onChange=\"objFrmSubm('reload');\" class='inpt01'>\n");
	$pos = 0;
	while($rowVar = $objSDAO->fetch("Variab",$pos)){
		$pos = '';
		if ($rowVar["layout_alias"]=='W'){ 
			$sel = "";
			if($variable == $rowVar["variable"])
				$sel = "SELECTED";
			echo("<option value=\"".$rowVar["variable"]."\" $sel>W_".$rowVar["variable"]."</option>\n");
		}
	}
	echo("</select><input type='hidden' name='attribute' value=''>\n");
} else {
	echo("<td class=\"labl01\">Attribute</td>" .
			"<td class=\"text01\" colspan='1'>\n");
	echo("<select name=\"attribute\" id=\"attribute\" onChange=\"objFrmSubm('reload');\" class='inpt01'>\n");
	$rowAtt = $objSDAO->fetch("Attr",0);
	while($rowAtt){
		$sel = "";
		if($attribute == $rowAtt["attribute"])
			$sel = "SELECTED";
		echo("<option value=\"".$rowAtt["attribute"]."\" $sel>".$alias_entity[0].
			"_".$rowAtt["attribute"]."</option>\n");
		$rowAtt = $objSDAO->fetch("Attr");
	}
	echo("</select><input type='hidden' name='variable' value=''>\n");
}
echo("</td>\n");
if($ls_type!='table-line') {
	// mode- tipo
	echo("<td class=\"labl01\">Mode</td>" .
				"<td class=\"text01\" colspan='1'>");
	echo("<select name=\"field_mode\" id=\"field_mode\" class='inpt01'>");
	foreach($aValFieldMode as $val){
		$sel = "";
		if($val == $field_mode)
			$sel = "SELECTED";
		echo("<option value=\"$val\" $sel>$val</option>");
	}
	echo("</select></td>\n");
} else {
	echo("<td class=\"text01\" colspan='2'>&nbsp;".
			"<input type='hidden' name=\"field_mode\" id=\"field_mode\" value='display'>".
			"<input type='hidden' name=\"field_type\" id=\"field_type\" value='text'></td>\n");
}
echo("</tr>\n");	

if ($rowLookup) { // define lookup
	echo("<tr>" .
				"<td class=\"labl01\">Look-up</td>" .
				"<td class=\"text01\" colspan='5'>");
	echo("<input type='text' name='lookup_sep1' id='lookup_sep1' value=\"".
					sxHE($lookup_sep1)."\" class='inpt01' size='3' maxlength='16'>\n");
	echo("<select name=\"lookup_attr1\" id=\"lookup_attr1\" class='inpt01'>\n");
	echo("<option value=\"\">----</option>");
	$rowLookup = $objSDAO->fetch("lookup",0);
	while($rowLookup){
		$sel = "";
		if($lookup_attr1 == $rowLookup['attribute'])
			$sel = "SELECTED";
		echo("<option value=\"".$rowLookup['attribute']."\" $sel>".$rowLookup['attribute']."</option>");
		$rowLookup = $objSDAO->fetch("lookup");
	}
	echo("</select>\n");
	echo("<input type='text' name='lookup_sep2' id='lookup_sep2' value=\"".
					sxHE($lookup_sep2)."\" class='inpt01' size='3' maxlength='16'>\n");
	echo("<select name=\"lookup_attr2\" id=\"lookup_attr2\" class='inpt01'>\n");
	echo("<option value=\"\">----</option>");
	$rowLookup = $objSDAO->fetch("lookup",0);
	while($rowLookup){
		$sel = "";
		if($lookup_attr2 == $rowLookup['attribute'])
			$sel = "SELECTED";
		echo("<option value=\"".$rowLookup['attribute']."\" $sel>".$rowLookup['attribute']."</option>");
		$rowLookup = $objSDAO->fetch("lookup");
	}
	echo("</select>\n");
	echo("<input type='text' name='lookup_sep3' id='lookup_sep3' value=\"".
					sxHE($lookup_sep3)."\" class='inpt01' size='3' maxlength='16'>\n");
	echo("<select name=\"lookup_attr3\" id=\"lookup_attr3\" class='inpt01'>\n");
	echo("<option value=\"\">----</option>");
	$rowLookup = $objSDAO->fetch("lookup",0);
	while($rowLookup){
		$sel = "";
		if($lookup_attr3 == $rowLookup['attribute'])
			$sel = "SELECTED";
		echo("<option value=\"".$rowLookup['attribute']."\" $sel>".$rowLookup['attribute']."</option>");
		$rowLookup = $objSDAO->fetch("lookup");
	}
	echo("</select>\n");
	echo("<input type='text' name='lookup_sep4' id='lookup_sep4' value=\"".
					sxHE($lookup_sep4)."\" class='inpt01' size='3' maxlength='16'>\n");
	echo("</td>\n</tr>\n");	
}

// field properties
echo("<tr>" .
			"<td class=\"labl01\">Display field properties</td>" .
			"<td class=\"text01\">");
echo("<select name=\"field_attr\" id=\"field_attr\" class='inpt01'>");
foreach($aValFieldAttr as $attr){
	$sel = "";
	if($attr == $field_attr)
		$sel = "SELECTED";
	echo("<option value=\"$attr\" $sel>$attr</option>");
}
echo("</select></td>\n");
echo("<td class=\"labl01\">Alignement</td>" .
			"<td class=\"text01\">");
echo("<select name=\"field_align\" id=\"field_align\">");
foreach($aValFieldAlign as $attr){
	$sel = "";
	if($attr == $field_align)
		$sel = "SELECTED";
	echo("<option value=\"$attr\" $sel>$attr</option>");
}
echo("</select></td>\n");
echo("<td class=\"labl01\">Style</td>" .
			"<td class=\"text01\">");
echo("<select name=\"field_style\" id=\"field_style\" class='inpt01'>");
foreach($aValFieldStyle as $attr){
	$sel = "";
	if($attr == $field_style)
		$sel = "SELECTED";
	echo("<option value=\"$attr\" $sel>$attr</option>");
}
echo("</select>");
echo("</td>" .
		"</tr>\n");
// field format
echo("<tr>" .
			"<td class=\"labl01\">Dispaly field format</td>" .
			"<td class=\"text01\">");
echo("<select name=\"field_format\" id=\"field_format\" class='inpt01'>");
echo("<option value=\"\">----</option>");
foreach($aValFieldFormat as $val){
	$sel = "";
	if($val == $field_format)
		$sel = "SELECTED";
	echo("<option value=\"$val\" $sel>$val</option>");
}
echo("</select></td>\n");
echo("<td class=\"labl01\">Parameters</td>" .
			"<td class=\"text01\" colspan='3'><input class=\"inpt01\" type=\"text\" name=\"field_format_par\" id=\"field_format_par\" size=\"64\" maxlength='128' value=\"$field_format_par\"></td>" .
		"</tr>\n");
// input type
echo("<tr>\n" .
			"<td class=\"labl01\">Input type</td>" .
			"<td class=\"text01\" colspan='1'>");
echo("<select name=\"field_type\" id=\"field_type\" class='inpt01'>");
foreach($aValFieldType as $attr){
	$sel = "";
	if($attr == $field_type)
		$sel = "SELECTED";
	echo("<option value=\"$attr\" $sel>$attr</option>");
}
echo("</select>");
echo("</td>" .
			"<td class=\"labl01\">Width</td>" .
			"<td class=\"text01\"><input class=\"inpt01\" type=\"text\" name=\"field_width\" " .
			"id=\"field_width\" size=\"5\" value=\"$field_width\"></td>\n");
echo("<td class=\"labl01\">Height</td>" .
			"<td class=\"text01\" colspan='1'><input class=\"inpt01\" type=\"text\" name=\"field_height\" id=\"field_height\" size=\"5\" value=\"$field_height\"></td>");
echo("</tr>\n");
// value array onChange
echo("<tr>\n" .
			"<td class=\"labl01\">Value array</td>" .
			"<td class=\"text01\" colspan='2'>");
echo("<select name=\"values_array\" id=\"values_array\" class='inpt01'>\n");
echo("<option value=\"\" >---</option>\n");
$pos = 0;
while($rowVar = $objSDAO->fetch("Variab",$pos)) {
	$pos = '';
	if ($rowVar["layout_alias"] != 'X') {continue;}
	$sel = "";
	if($values_array == $rowVar["variable"])
		$sel = "SELECTED";
	echo("<option value=\"".$rowVar["variable"]."\" $sel>X_".$rowVar["variable"]."</option>\n");
}
echo("</select></td>\n");
echo("</td>" .
			"<td class=\"labl01\" colspan='2' align='right'>Reload if value changed : </td>" .
			"<td class=\"text01\">" .
			"<input class=\"text01\" type=\"radio\" name=\"on_change_reload\" id=\"on_change_reload\"".
			" value='Y' ".(($on_change_reload=='Y')?'CHECKED':'')."> yes &nbsp;&nbsp;&nbsp;" .
			"<input class=\"text01\" type=\"radio\" name=\"on_change_reload\" id=\"on_change_reload\"".
			" value='N' ".(($on_change_reload=='N')?'CHECKED':'')."> No &nbsp;&nbsp;&nbsp;" .
			"</td>\n");
echo("</tr>\n");
// check variable
echo "<tr>";
echo("<td class=\"labl01\" colspan='1'>Enabling values</td>" .
		"<td class=\"text01\" colspan='2'>\n");
echo("<select name=\"check_variable\" id=\"check_variable\" class='inpt01'>\n");
echo("<option value=\"\" >---</option>\n");
$pos = 0;
while($rowVar = $objSDAO->fetch("Variab",$pos)) {
	$pos = '';
	if ($rowVar["layout_alias"] != 'W') {continue;}
	$sel = "";
	if($check_variable == $rowVar["variable"])
		$sel = "SELECTED";
	echo("<option value=\"".$rowVar["variable"]."\" $sel>W_".$rowVar["variable"]."</option>\n");
}
echo "</select></td>\n";
echo "<td class=\"labl01\" colspan='2' align='right'>Default validation : </td>" .
			"<td class=\"text01\">";
echo("<select name=\"field_check\" id=\"field_check\" class='inpt01'>");
echo("<option value=\"\">----</option>");
foreach($aValFieldCheck as $val){
	$sel = "";
	if($val == $field_check)
		$sel = "SELECTED";
	echo("<option value=\"$val\" $sel>$val</option>");
}
echo("</select></td>\n");
echo "</tr>\n";
// upgrade status
$aVal = array('S'=>'Strutturale',
	'M'=>'Modificabile non cancellabile',
	'C'=>'Modificabile e cancellabile',
	'D'=>'Cancellato',
	'U'=>'Modificato');
echo("<tr>" .
			"<td class=\"labl01\">Upgrade status</td>" .
			"<td class=\"text01\" colspan='5'>".getHTMLRadio('upgrade_status', $upgrade_status, $aVal)."</td>" .
		"</tr>\n");
// value list
echo("<tr>" .
			"<td class=\"labl01\">List of values</td>" .
			"<td class=\"text01\" colspan='5'><textarea class=\"inpt01\" name=\"field_value_list\" id=\"field_value_list\" cols=\"80\" rows=\"2\">$field_value_list</textarea></td>" .
		"</tr>\n");		
// predefined HTML 
echo("<tr>" .
			"<td class=\"titl01\" colspan='6'>Predefined HTML </td>" .
		"</tr>\n");
// string HTML 
echo("<tr>" .
			"<td class=\"labl01\">HTML string</td>" .
			"<td class=\"text01\" colspan='5'><textarea class=\"inpt01\" name=\"html_string\" id=\"html_string\" cols=\"80\" rows=\"8\">".sxHE($html_string)."</textarea></td>" .
		"</tr>\n");			
echo("</table>");
echo $sCmd;
echo("</form>");
echo(footerHTML());
exit();

function layoutBandFieldIns($redirOnEnd = true){
 	global $objSDAO;
	$errMsg = "";
	if(sxREQ('row') == "")
		$errMsg .= "missing row<br>";
	if(preg_match("/[^0-9]/",sxREQ('row')))
		$errMsg .= "not numeric row<br>";
	if(sxREQ('seq') == "")
		$errMsg .= "missing seq.<br>";
	if(preg_match("/[^0-9]/",sxREQ('seq')))
		$errMsg .= "not numeric seq.<br>";
	if(preg_match("/[^0-9]/",sxREQ('label_colspan')))
		$errMsg .= "not numeric label_colspan<br>";
	if(preg_match("/[^0-9]/",sxREQ('label_colspan')))
		$errMsg .= "not numeric label_colspan<br>";
	if(preg_match("/[^0-9]/",sxREQ('field_colspan')))
		$errMsg .= "not numeric field_colspan<br>";
	if(preg_match("/[^0-9]/",sxREQ('field_width')))
		$errMsg .= "not numeric field_width<br>";
	if(preg_match("/[^0-9]/",sxREQ('field_height')))
		$errMsg .= "not numeric field_height<br>";
	if (sxREQUEST('field_colspan')+sxREQUEST('label_colspan') <= 0) {
		$errMsg .= _t("numero colonne campo deve essere > 0").".<br>";
	}
	$field = sxRAS('layout')."/".intval(sxRAS('band'))."/".intval(sxRAS('row'))."/".intval(sxRAS('seq'));
	if($errMsg != "")
		return $errMsg; 
	//existence
	$sSQL = "SELECT count(*) as num FROM layout_field " .
				"WHERE layout = ".intval(sxRAS('layout'))." " .
						"AND band = ".intval(sxRAS('band'))." " .
						"AND row = ".intval(sxRAS('row'))." AND seq = ".intval(sxRAS('seq'));
	$objSDAO->query($sSQL,"test");
	$row = $objSDAO->fetch("test");
	if($num = $row["num"] != 0){
		$errMsg .= _t("existing field %s",$campo)."<br>";
		return $errMsg;
	}
	//check for space
	$sSQL = "SELECT lf.*, l.max_colspan FROM layout l " .
				" LEFT JOIN layout_field lf ON lf.layout=l.layout " .
						" AND lf.band=".intval(sxRAS('band'))." " .
						" AND lf.row=".intval(sxRAS('row'))." " .
				" WHERE l.layout = '".sxRAS('layout')."'";
	$objSDAO->query($sSQL,"test");
	$row = $objSDAO->fetch("test");
	$numCelleRes = $row["max_colspan"];
	$label_colspan = intval(sxREQUEST("label_colspan"));
	$field_colspan = intval(sxREQUEST("field_colspan"));
	
	while($row){
		$numCelleRes = $numCelleRes - $row["label_colspan"]  - $row["field_colspan"]; 
		$row = $objSDAO->fetch("test");
	}
	if($numCelleRes - $label_colspan - $field_colspan < 0){
		$errMsg .= _t("not enough space for field '$field'. (max %s cols.)",$numCelleRes)."<br>";
	}
	if($errMsg != ""){return $errMsg;}		
	
	//band existence
	$sSQL = "SELECT count(*) AS num FROM layout_band " .
				"WHERE layout=".intval(sxRAS('layout'))." " .
						"AND band=".intval(sxRAS('band'));
	$objSDAO->query($sSQL,"test");
	$row = $objSDAO->fetch("test");
	if ($row['num'] == 0) {
		$sSQL = "INSERT INTO layout_band (" .
				"layout, " .
				"band, " .
				"flags, " .
				"description, " .
				"note) " .
				" VALUES('".sxRAS("layout")."', " .
				" ".sxRAS("band").", " .
				"'', " .
				"'BAND".sxRAS("band")."', " .
				"''" .
				")";	
		$objSDAO->query($sSQL,"ins");
	}
		
	$sSQL = "INSERT INTO " .
			"layout_field " .
			"(layout, " .
			"band, " .
			"row, " .
			"seq, " .
			"layout_alias, " .
			"entity, " .
			"attribute, " .
			"variable, " .
			"label_colspan, " .
			"label, " .
			"label_attr, " .
			"label_align, " .
			"label_style, " .
			"field_mode, " .
			"field_type, " .
			"field_format, " .
			"field_format_par, " .
			"field_value_list, " .
			"field_attr, " .
			"field_align, " .
			"field_style, " .
			"field_width, " .
			"field_height, " .
			"field_colspan, " .
			"check_variable, " .
			"field_check, ".
			"lookup_sep1, ".
			"lookup_sep2, ".
			"lookup_sep3, ".
			"lookup_sep4, ".
			"lookup_attr1, ".
			"lookup_attr2, ".
			"lookup_attr3, ".
			"values_array, ".
			"on_change_reload, ".
			"upgrade_status, ".
			"html_string)" .
			"VALUES('" .sxRAS("layout")."', " .
			sxRint("band").", " .
			sxRint("row").", " .
			sxRint("seq").", " .
			"'".substr(sxRAS('alias_entity'),0,1)."', " .
			"'".substr(sxRAS('alias_entity'),2)."', " .
			"'".sxRAS('attribute')."', " .
			"'".sxRAS('variable')."', " .
			intval(sxRAS('label_colspan')).", " .
			"'".sxRAS('label')."', " .
			"'".sxRAS('label_attr')."', " .
			"'".sxRAS('label_align')."', " .
			"'".sxRAS("label_style")."', " .
			"'".sxRAS('field_mode')."', " .
			"'".sxRAS('field_type')."', " .
			"'".sxRAS('field_format')."', " .
			"'".sxRAS('field_format_par')."', " .
			"'".sxRAS('field_value_list')."', " .
			"'".sxRAS('field_attr')."', " .
			"'".sxRAS('field_align')."', " .
			"'".sxRAS("field_style")."', " .
			intval(sxRAS("field_width")).", " .
			intval(sxRAS("field_height")).", " .
			intval(sxRAS("field_colspan")).", " .
			"'".sxRAS('check_variable')."', " .
			"'".sxRAS('field_check')."', " .
			"'".sxRAS('lookup_sep1')."', " .
			"'".sxRAS('lookup_sep2')."', " .
			"'".sxRAS('lookup_sep3')."', " .
			"'".sxRAS('lookup_sep4')."', " .
			"'".sxRAS('lookup_attr1')."', " .
			"'".sxRAS('lookup_attr2')."', " .
			"'".sxRAS('lookup_attr3')."', " .
			"'".sxRAS('values_array')."', " .
			"'".sxRAS('on_change_reload')."', " .
			"'".sxRAS('upgrade_status')."', " .
			"'".sxRAS('html_string')."'" .				
			")";	
	$objSDAO->query($sSQL,"ins");
	$url = $_SESSION[USEROBJ]->getHistory(0, true);
	serverRedir($url);
	return 'abnormal end';
}

